// uhttp.js
const config = {
	baseUrl: 'https://api.zhihuiyun.life/api',
	showLoading: false,
	loadingMask: false,
	originalData: true
}

const install = (Vue, vm) => {
	Vue.prototype.$u.http.setConfig(config);

	// 请求拦截
	Vue.prototype.$u.http.interceptor.request = (config) => {
		let token = vm.$store.state.token
		if (token) {
			config.header.Authorization = 'Bearer ' + token
		} else {
			config.header.Authorization = ''
		}
		return config;
	}

	// 响应拦截
	Vue.prototype.$u.http.interceptor.response = (res) => {
		if (res.data.success) {
			if (res.data.code == 0) {
				return res
			} else if (res.data.code == -1) {
				vm.$msg(res.data.msg)
			}
		} else {
			if (res.data.code == 401) {
				vm.$msg('未登录或已过期', 1000, true).then(vm.$goto('/pages/account/login'))
			} else {
				vm.$msg(res.data.msg)
			}
		}
	}

	// 结束
}

export default {
	install
}
